package com.itheima.bakery.demos.web.controller;

import com.itheima.bakery.demos.web.model.ShippingAddress;
import com.itheima.bakery.demos.web.service.ShippingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shipping-addresses")
public class ShippingAddressController {

    @Autowired
    private ShippingAddressService shippingAddressService;

    // 获取所有收货地址
    @GetMapping
    public List<ShippingAddress> getAllShippingAddresses() {
        return shippingAddressService.getAllShippingAddresses();
    }

    // 根据ID获取收货地址
    @GetMapping("/{id}")
    public ShippingAddress getShippingAddressById(@PathVariable Integer id) {
        return shippingAddressService.getShippingAddressById(id);
    }

    // 根据用户ID获取收货地址
    @GetMapping("/user/{userId}")
    public List<ShippingAddress> getShippingAddressesByUserId(@PathVariable Integer userId) {
        return shippingAddressService.getShippingAddressesByUserId(userId);
    }

    // 创建收货地址
    @PostMapping
    public ShippingAddress createShippingAddress(@RequestBody ShippingAddress shippingAddress) {
        return shippingAddressService.createShippingAddress(shippingAddress);
    }

    // 更新收货地址
    @PutMapping("/updateaddress/{id}")
    public ShippingAddress updateShippingAddress( @PathVariable Integer id, @RequestBody ShippingAddress shippingAddress) {
        return shippingAddressService.updateShippingAddress(id, shippingAddress);
    }
    // 删除收货地址
    @DeleteMapping("/{id}")
    public void deleteShippingAddress(@PathVariable Integer id) {
        shippingAddressService.deleteShippingAddress(id);
    }
}